package com.sense.kukoo.biz_explore.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_explore.model.Explore2Model
import com.sense.kukoo.common.event.LoginSuccessEvent
import com.sense.kukoo.common.event.LogoutEvent
import com.sense.kukoo.common.event.MessageCountChangedEvent
import com.sense.kukoo.common.event.TabChangedEvent
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.http.bean.ModuleBean
import com.sense.kukoo.common.repo.http.bean.UserMessagesBean
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class Explore2ViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mUserMessagesBean: UserMessagesBean

    val action_toSearch:MutableLiveData<Boolean> = MutableLiveData()
    val action_toNotification:MutableLiveData<Boolean> = MutableLiveData()
    val action_showLoginDialog:MutableLiveData<Boolean> = MutableLiveData()
    val action_showTopTab:MutableLiveData<Boolean> = MutableLiveData()
    val action_networkError: MutableLiveData<Boolean> = MutableLiveData()
    val data_index:MutableLiveData<List<ModuleBean>> = MutableLiveData()
    val data_messageCount:MutableLiveData<Int> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {
        EventBus.getDefault().register(this)
        //
        getIndex()
        if (Repo.login.isLogin()){
            getMessageCount()
        }
    }

    fun onClickSearch(){
        action_toSearch.postValue(true)
    }

    fun onClickNotification(){
        if (Repo.login.isLogin()){
            action_toNotification.postValue(true)
        } else {
            action_showLoginDialog.postValue(true)
        }
    }

    fun getIndex(){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val result = Repo.http.indexModule()
            if (result.isSuccess()) {
                result.data?.let {
                    data_index.postValue(it)
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun getMessageCount(){
        request {
            val result = Repo.http.messageCount()
            if (result.isSuccess()) {
                result.data?.let {
                    mUserMessagesBean = it
                    //
                    EventBus.getDefault().post(MessageCountChangedEvent(mUserMessagesBean))
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    private fun showMessageCount(){
        mUserMessagesBean.let {
            var count = 0
            count += it.likeNum
            count += it.subscriptionNum
            count += it.commentNum
            count += it.systemNum
            count += it.creatorNum
            //
            data_messageCount.postValue(count)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageCountChangedEvent) {
        mUserMessagesBean = event.data
        showMessageCount()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: LoginSuccessEvent) {
        getMessageCount()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: LogoutEvent) {
        data_messageCount.postValue(0)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: TabChangedEvent) {
        action_showTopTab.postValue(true)
    }

    override fun onCleared() {
        super.onCleared()
        //
        EventBus.getDefault().unregister(this)
    }

}